package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CourseTeacherServiceImpl implements CourseTeacherService {
   @Autowired
    CourseTeacherMapper courseTeacherMapper;
   @Autowired
    CourseBaseMapper courseBaseMapper;

    /**
     * 查询课程id教师信息
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> getTeacherInfo(Long courseId) {
        List<CourseTeacher> courseTeach=courseTeacherMapper.selectByCourseId(courseId);
        return courseTeach;
    }

    /**
     * 新增或修改教师信息
     * @param courseTeacher
     * @param companyId
     * @return
     */
    @Override
    public CourseTeacher addAndUpdateCourseTeacher(CourseTeacher courseTeacher, Long companyId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if (courseBase==null){
            XueChengPlusException.cast("课程不存在");
        }
        if (!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("本机构只能修改本机构课程");
        }
        CourseTeacher teacherNew = new CourseTeacher();
        //拷贝前端传递过来的数据
        BeanUtils.copyProperties(courseTeacher,teacherNew);
        //判断是新增教师还是修改教师信息
        if (courseTeacher.getId()!=null){
           courseTeacherMapper.updateById(teacherNew);
        }else {
            //TODO 设置教师头像
            teacherNew.setPhotograph("null");
            teacherNew.setCreateDate(LocalDateTime.now());
            //插入成功后数据库会返回id给实体DTO
            courseTeacherMapper.insert(teacherNew);
        }

        //查询教师信息并返回
        return courseTeacherMapper.selectById(teacherNew.getId());
    }


    /**
     * 删除课程老师
     * @param courseId
     * @param id
     */
    @Override
    public void removeCourseTeacher(Long courseId, Long id ,Long companyId) {
        //校验数据合理性
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        CourseTeacher teacher = courseTeacherMapper.selectById(id);
        if (teacher==null||courseBase==null){
            XueChengPlusException.cast("教师或者课程不存在");
        }
        if (!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("本机构只能修改本机构课程");
        }
            LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseTeacher::getCourseId,courseId)
                    .eq(CourseTeacher::getId,id);
            courseTeacherMapper.delete(queryWrapper);
        }

}
